﻿using DevExpress.XtraGrid.Demos.util;
using Luna小店.DTO;
using Luna小店.model;
using Luna小店.service;
using Luna小店.UC.款式异常;
using Luna小店.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Luna小店.UC.款式管理
{
    public partial class Frm款式面料设计点管理 : DevExpress.XtraEditors.XtraForm
    {
        public 门店下单选款式Model model = new 门店下单选款式Model();
        //public List<设计点图片Dto> 设计点图片Dtos = new List<设计点图片Dto>();
        public List<DTO款式配置设计点展现> 设计点图片Dtos = new List<DTO款式配置设计点展现>();
        public String STYLE_ID;
        public String STYLE_NAME_CN;
        public Frm款式面料设计点管理()
        {
            InitializeComponent();
            this.model = new 门店下单选款式Model("",0);
            this.gridControl款式一览.DataSource = this.model.款式图片;
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            this.STYLE_NAME_CN = this.gridView款式一览.GetRowCellValue(e.RowHandle, "StyleNameCn").ToString();
            this.STYLE_ID = this.gridView款式一览.GetRowCellValue(e.RowHandle, "StyleId").ToString();
            this.设计点图片Dtos = ItemService.GetDTOsByStyleID(this.STYLE_ID);
            //款式配置设计点数据清洗Model mod = new 款式配置设计点数据清洗Model(this.设计点图片Dtos);
            //String str = FunctionHelper.JsonSerialization(mod.DTO款式配置设计点数据清洗s);
            this.gridView款式一览.GroupPanelText = "当前选中款式为：" + this.STYLE_NAME_CN;
            this.gridControl设计点一览.DataSource = this.设计点图片Dtos;
            this.gridControl设计点一览.Refresh();
        }

        private void textBox款式检索_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13) //判断是回车键
            {
                this.model = new 门店下单选款式Model(this.textBox款式检索.Text, 0);
                this.gridControl款式一览.DataSource = this.model.款式图片;
                this.gridControl款式一览.Refresh();
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if(ItemService.SaveExcelToTable(设计点图片Dtos, this.STYLE_ID,this.STYLE_NAME_CN,true)==true)
            {
                MessageBox.Show("上传成功");
            }
            else
            {
                MessageBox.Show("上传失败");
            }
        }

        private void simpleButton下载_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls;*.xlsx)|*.xls;*xlsx";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                try
                {
                    DataTable dt = DataTableExtend.ToDataTable<设计点Excel格式>(ItemService.ListForExcel(this.设计点图片Dtos));
                    DataTableExtend.SaveAsXlS(fileDialog.FileName, dt);
                    //ToExcel.SaveToExcel(fileDialog.FileName, dt);
                    //DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                    //gridControl1.ExportToXls(fileDialog.FileName);
                    DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("保存失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }
}
